package com.DFB.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/getMerchantImages")
public class staticController {


    //商户获取图片
    @GetMapping("merchant/{imageName}")
    public ResponseEntity<Resource> getMerchantImage(@PathVariable String imageName) throws IOException {
        ClassPathResource imageResource = new ClassPathResource("images/merchant/" + imageName);

        if (!imageResource.exists()) {
            log.info("图片加载失败：{}", "images/merchant" + imageName);
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .body(new InputStreamResource(imageResource.getInputStream()));
    }


    //获取商品图片
    @GetMapping("product/{imageName}")
    public ResponseEntity<Resource> getProductImage(@PathVariable String imageName) throws IOException {
        ClassPathResource imageResource = new ClassPathResource("images/product/" + imageName);

        if (!imageResource.exists()) {
            log.info("图片加载失败：{}", "images/product" + imageName);
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .body(new InputStreamResource(imageResource.getInputStream()));
    }


}